
local excel = load_excel('商店设置.xlsx','神秘商店')
local weights = {}
local rty = {
    ['金币'] = 'gold',
    ['木材'] = 'lum',
    ['杀敌'] = 'food',
}
for name,data in pairs(excel) do
    local num,ty = data['消耗']:match('(%d+)(.*)')
    local key = rty[ty] or '金币'
    data[key] = tonumber(num) or 0
    weights[#weights + 1] = {name,data['权重'] or 10}

    ac.wait(100,function()
        local item = ac.item[name]
        if item then
            item.art = data.art
            item.tip = data.tip
            item[key] = data[key]
        end
    end)
end

local zb = {}
for key,v in pairs(load_excel('特殊奖池.xlsx','神秘商店')) do
    local data1 = v['参数1']
    local data2 = v['参数2']
    local data3 = v['参数3']
    local data4 = v['参数4']
    local data5 = v['参数5']
    zb[#zb + 1] = {data1,data2,data3 or 0}
end

ac.skill('神秘商店物品'){
    keep_cool = false,
    ['强制购买'] = true,
    sdata1 = function(self)
        local unit = self.owner
        local name = self.bind_name or self.name
        local count = unit:get_data(name,0)
        if name=='全属性之书' then
            return math.min(60 + 6 * count, 120)
        end
        return math.min(100 + 10 * count, 200)
    end,
    get_food = function(self,player)
        local player = player or ac.player.self
        return self.food * (1 - player:get_data('黑市折扣',0)/100)
    end,
    on_add = function(self)
        local name = self.bind_name or self.name
        local item = ac.item.new(name) or excel[name]
        if item then
            self.art = item.art
            self.title = item.title or name
            self.show_effect = format_obj(item,item.show_effect)
            if excel[name] and name~='觉醒石' then
                self.tip = item.tip
            else
                self.tip = format_obj(item,item.tip)
            end
        end
        self.lum = item.lum or 0
        self.gold = item.gold or 0
        self.food = item.food or 0
        self.bind_item = item
        self:set_title(self.title or self.name)
        self:init_data()
    end,
    on_sell = function(self,hero,player)
        local unit = self.owner
        local name = self.bind_name or self.name
        local player = unit.owner
        local hero = player.hero
        self:remove()
        
        local count = unit:get_data(name,0)
        unit:set_data(name,count + 1)
        hero:notify('单位-购买神秘商店物品',hero,self)

        show_skill_tip(player,self,'刷新')
        
        if ac.item[name] then
            local info = self.bind_item or {owner_player = buyer.owner}
            local item = hero:add_item(name,info)
            hero_add_item(hero,item)
            return
        end

        if name=='真理之剑' or name=='奥义法杖' then
            if name=='真理之剑' then
                hero:add('物理伤害',5)
            end
            if name=='奥义法杖' then
                hero:add('魔法伤害',5)
            end
            if unit:get_data('真理之剑',0)==1 and unit:get_data('奥义法杖',0)==1 then
                hero:add('伤害增幅',15)
                player:send_msg('集齐真理之剑/奥术法杖，获得奖励伤害增幅+15%')
            end
        elseif name=='金币福袋' then
            local gold = math.random(1000,4000)
            player:add('金币',gold)
            player:send_msg(('获得：|cffffff00%s|r金币'):format(gold))
            
        elseif name=='杀敌福袋' then
            local gold = math.random(0,1000)
            player:add('杀敌',gold)
            player:send_msg(('获得：|cffff0000%s|r杀敌'):format(gold))
            
        elseif name=='力量之书' then
            local num = self:sdata1()
            hero:add('基础力量',num)
            player:send_msg(('力量+%s'):format(num))
            
        elseif name=='敏捷之书' then
            local num = self:sdata1()
            hero:add('基础敏捷',num)
            player:send_msg(('敏捷+%s'):format(num))
            
        elseif name=='智力之书' then
            local num = self:sdata1()
            hero:add('基础智力',num)
            player:send_msg(('智力+%s'):format(num))

        elseif name=='全属性之书' then
            local num = self:sdata1()
            hero:add('基础力量',num)
            hero:add('基础敏捷',num)
            hero:add('基础智力',num)
            player:send_msg(('全属性+%s'):format(num))

        elseif name=='力量之石' then
            hero:add('力量加成',3)
            
        elseif name=='敏捷之石' then
            hero:add('敏捷加成',3)

        elseif name=='智力之石' then
            hero:add('智力加成',3)

        elseif name=='全能之石' then
            hero:add('力量加成',1)
            hero:add('敏捷加成',1)
            hero:add('智力加成',1)
        elseif name=='装备礼盒*小' then
            for a=1,5 do
                local name = random_item_name(math.random(3))
                local it = ac.item.create(name,nil,{owner_player = player})
                collate_item(hero,it)
                player:send_msg(('获得:%s'):format(it:get_title()))
            end
        elseif name=='装备礼盒*中' then
            for a=1,5 do
                local name = random_item_name(math.random(2,4))
                local it = ac.item.create(name,nil,{owner_player = player})
                collate_item(hero,it)
                player:send_msg(('获得:%s'):format(it:get_title()))
            end
        elseif name=='装备礼盒*大' then
            for a=1,5 do
                local name = random_item_name(math.random(3,5))
                local it = ac.item.create(name,nil,{owner_player = player})
                collate_item(hero,it)
                player:send_msg(('获得:%s'):format(it:get_title()))
            end
        elseif name=='黄金矿工' then
            player:notify('玩家-添加矿工',player,5)
        end
    end
}

--更新商店
local only = {
    ['真理之剑'] = true,
    ['奥义法杖'] = true,
}
local function update_shop(unit)
    for skl in unit:each_skill() do
        if skl.name~='刷新神秘商店' then
            skl:remove()
        end
        if only[skl.bind_name] then
            local data = excel[skl.bind_name]
            table.insert(unit.weights,{skl.bind_name,data['权重'] or 10})
        end
    end

    local player = unit.owner
    local num = 4 + player:get_data('黑市额外物品',0)
    for a=1,num do
        local name,index = table.get_by_weight(unit.weights)
        if only[name] then
            table.remove(unit.weights,index)
        end
        if name=='随机装备' then
            local lv = ac.enemy and ac.enemy.boss_index or 1
            local weight = {}
            for _,da in ipairs(zb) do
                weight[#weight + 1] = {da[1],math.max(da[2] + da[3]*lv,0)}
            end

            local lv = table.get_by_weight(weight)
            name = random_item_name(lv)
        end
        unit:add_skill('神秘商店物品','英雄',a,{bind_name = name})
    end

end


local update_time = 90
local wait_update = nil
wait_update = function(unit,time)
    local player = unit.owner
    if unit.update_timer then
        unit.update_timer:remove()
    end
    unit.update_timer = unit:wait(time*1000,function(t)
        t:remove()
        if unit.update_skill then
            unit.update_skill:remove()
        end
        unit.update_skill = unit:add_skill('刷新神秘商店','英雄',9)
        update_shop(unit)
        wait_update(unit,update_time)
        player:notify('UI-刷新动画',player,unit)
    end)
end

ac.skill('刷新神秘商店'){
    title = [[黑市刷新]],
    count = 1,
    art = [[ReplaceableTextures\CommandButtons\BTNPatrol.blp]],
    tip = [[刷新商店]],
    get_food = function(self)
        return self.count * 100
    end,
    on_sell = function(self)
        local unit = self.owner
        self.count = self.count + 1
        update_shop(unit)
    end,
}

ac.game:event '玩家-创建神秘商店'(function(_,player)
    if player:get_data('神秘商店') then
        return
    end
    local unit = player:create_unit('神秘商店',player.shop_point2,270)
    player:set_data('神秘商店',unit)
    unit.weights = table.copy(weights)
    shop_add_panel(unit)
    unit:set_data('血条贴图',[[core\hpbar\ShieldBar.tga]])
    unit:set('生命上限',update_time)
    unit:loop(30,function()
        if unit.update_timer then
            local time = unit.update_timer:get_remaining()/1000
            unit.ui_title = ('神秘商店(|cff00ff00%0.f|r)'):format(time)
            unit:set('生命',time)
        end
    end)
    wait_update(unit,0)
end)


ac.item('木头挑战'){
    item_type = "charged",
    auto_merge = true,
    model = [[31.mdl]],
    tip = [[召唤大量怪物，击杀获得大量木头]],
    on_use = function(self)
        local unit = self.owner
        local player = unit.owner
        ac.timer(500,10,function()
            local unit = player:create_enemy(ac.enemy.elite_name)
            unit['木材'] = 10
        end)
    end
}

ac.item('探索挑战'){
    item_type = "charged",
    auto_merge = true,
    model = [[177.mdl]],
    tip = [[召唤探索怪物，击杀获得大量属性，概率掉落装备]],
    on_use = function(self)
        local unit = self.owner
        local player = unit.owner
        local hero = player.hero
        local skill = hero:find_skill('探索挑战')
        if skill then
            skill:on_cast_shot()
            skill.count = skill.count - 1
        end
    end
}

ac.item('装备礼盒*小'){
    item_type = "charged",
    auto_merge = true,
    min = 1,
    max = 3,
    model = [[199.mdl]],
    color = 0xff00ff00,
    on_use = function(self)
        local unit = self.owner
        local player = unit.owner
        local hero = player.hero
        for a=1,5 do
            local name = random_item_name(math.random(self.min,self.max))
            local it = ac.item.create(name,nil,{owner_player = player})
            collate_item(hero,it)
            player:send_msg(('获得:%s'):format(it:get_title()))
        end
    end
}

ac.item('装备礼盒*中'){
    temp = '装备礼盒*小',
    color = 0xff00ccff,
    min = 2,
    max = 4,
}

ac.item('装备礼盒*大'){
    temp = '装备礼盒*小',
    color = 0xffca42fd,
    min = 3,
    max = 5,
}










